import * as readline from 'readline';
import * as fs from 'fs';
import * as path from 'path';

export async function readMarkdownFiles(dirPath:string): Promise<string[]> {
  try {
    const articlesDir = dirPath;
    
    // 检查目录是否存在
    if (!fs.existsSync(articlesDir)) {
      throw new Error(`articles 目录不存在: ${articlesDir}`);
    }

    // 读取目录下的所有文件
    const files = fs.readdirSync(articlesDir);
    
    // 过滤出 .md 文件
    const mdFiles = files.filter(file => file.toLowerCase().endsWith('.md'));
    
    // 读取每个文件的内容
    const contents = mdFiles.map(file => {
      const filePath = path.join(articlesDir, file);
      return fs.readFileSync(filePath, 'utf-8');
    });

    return contents;
  } catch (error) {
    console.error('读取 markdown 文件失败:', error);
    throw error;
  }
}
